package org.abettor.pushbox.view;

import org.abettor.pushbox.map.Point;
import org.abettor.pushbox.map.Step;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;

public class ShowResultView extends BoxView {
	public ShowResultView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public ShowResultView(Context context) {
		super(context);
	}

	/**
	 * 向前的一步动作
	 * 
	 * @param step
	 * @return
	 */
	public void forwardStep(Step step) {
		switch (step.getStatus()) {
		case Step.LEFT:
			left();
			break;
		case Step.RIGHT:
			right();
			break;
		case Step.DOWN:
			down();
			break;
		case Step.UP:
			up();
			break;
		}
	}

	/**
	 * 向后一步的操作
	 * 
	 * @param step
	 */
	public void backStep(Step current,Step pre) {
		// 初始时人的坐标
		int x = map.getPerson().getX();
		int y = map.getPerson().getY();
		map.revertPreStep(current, pre);
		// 弹出后人的坐标
		int x1 = map.getPerson().getX();
		int y1 = map.getPerson().getY();
		Canvas c1 = new Canvas(mPushBoxImage);
		if (x == x1) {
			drawEveryClip(c1, x, y);
			drawEveryClip(c1, x, y + 1);
			drawEveryClip(c1, x, y - 1);
		} else if (y == y1) {
			drawEveryClip(c1, x - 1, y);
			drawEveryClip(c1, x + 1, y);
			drawEveryClip(c1, x, y);
		}
		super.setImageBitmap(mPushBoxImage);
	}

	private void up() {
		Point point = map.getPerson();
		int x = point.getX();
		int y = point.getY();
		boolean flag = map.up();
		Canvas c1 = new Canvas(mPushBoxImage);
		if (!flag) {// 没有成功,绘制一个节点及可
			drawEveryClip(c1, x, y);
		} else {// 成功绘制三个节点
			drawEveryClip(c1, x, y);
			drawEveryClip(c1, x, y - 1);
			drawEveryClip(c1, x, y - 2);
		}
		super.setImageBitmap(mPushBoxImage);
	}

	private void left() {
		Point point = map.getPerson();
		int x = point.getX();
		int y = point.getY();
		boolean flag = map.left();
		Canvas c1 = new Canvas(mPushBoxImage);
		if (!flag) {// 没有成功,绘制一个节点及可
			drawEveryClip(c1, x, y);
		} else {// 成功绘制三个节点
			drawEveryClip(c1, x, y);
			drawEveryClip(c1, x - 1, y);
			drawEveryClip(c1, x - 2, y);
		}
		super.setImageBitmap(mPushBoxImage);
	}

	private void right() {
		Point point = map.getPerson();
		int x = point.getX();
		int y = point.getY();
		boolean flag = map.right();
		Canvas c1 = new Canvas(mPushBoxImage);
		if (!flag) {// 没有成功,绘制一个节点及可
			drawEveryClip(c1, x, y);
		} else {// 成功绘制三个节点
			drawEveryClip(c1, x, y);
			drawEveryClip(c1, x + 1, y);
			drawEveryClip(c1, x + 2, y);
		}
		super.setImageBitmap(mPushBoxImage);
	}

	private void down() {
		Point point = map.getPerson();
		int x = point.getX();
		int y = point.getY();
		boolean flag = map.down();
		Canvas c1 = new Canvas(mPushBoxImage);
		if (!flag) {// 没有成功,绘制一个节点及可
			drawEveryClip(c1, x, y);
		} else {// 成功绘制三个节点
			drawEveryClip(c1, x, y);
			drawEveryClip(c1, x, y + 1);
			drawEveryClip(c1, x, y + 2);
		}
		super.setImageBitmap(mPushBoxImage);
	}
}
